package ips.mod_stat.action;

import ips.mod_stat.pojo.modStat;
import ips.operate_log.dao.OplogDAO;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import java.net.UnknownHostException;

import org.hibernate.Session;
import org.hibernate.Transaction;

import com.opensymphony.xwork2.ActionSupport;

import cm.util.exception.MessageException;
import cm.util.hibernate.HibernateSessionFactory;

/**
 * 约定	modStat   o-关闭     1打开
 * 
 * 
 * 
 * 
 */
public class modstatinfo extends ActionSupport {

	private int id;
	private int mod;
	private int stat;
	private int modStat;
	private int modId;
	private int len;

	private String modName;
	private String modStat2;
	private String type;
	private String strrec;
	private String ip;
	private String address;
	
	DatagramSocket Socket ;
	
	private OplogDAO op=new OplogDAO();
	
	private byte result[] = new byte[12];

	private static final String OPEN = "open";

	private static final String CLOSE = "close";

	private static final String OPEN_SUCCESS = "open_success";

	private static final String CLOSE_SUCCESS = "close_success";
	
	private static final String OPEN_FAILED = "open_failed";
	
	private static final String CLOSE_FAILED = "close_failed";

	

	public String getIp() {
		return ip;
	}

	public void setIp(String ip) {
		this.ip = ip;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public int getModId() {
		return modId;
	}

	public void setModId(int modId) {
		this.modId = modId;
	}

	public String getModName() {
		return modName;
	}

	public void setModName(String modName) {
		this.modName = modName;
	}

	public Integer getModStat() {
		return modStat;
	}

	public void setModStat(int modStat) {
		this.modStat = modStat;
	}

	public String getModStat2() {
		return modStat2;
	}

	public void setModStat2(String modStat2) {
		this.modStat2 = modStat2;
	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

	public void setMod(int m) {
		mod = m;
	}

	public void setStat(int s) {
		stat = s;
	}

	public String execute() throws Exception {

		if (type == null || "".equals(type)) {
			throw new MessageException("操作类型不能为空！");
		}

		if (CLOSE.equals(type)) {
			return close();
		}
		if (OPEN.equals(type)) {
			return open();
		}

		return super.execute();
	}

	/**
	 * 数组转换成字节
	 */
	public void getbyte() {
		result[0] = (byte) mod;
		mod = mod >> 8;
		result[1] = (byte) mod;
		mod = mod >> 8;
		result[2] = (byte) mod;
		mod = mod >> 8;
		result[3] = (byte) mod;
		mod = mod >> 8;
		result[4] = (byte) stat;
		stat = stat >> 8;
		result[5] = (byte) stat;
		stat = stat >> 8;
		result[6] = (byte) stat;
		stat = stat >> 8;
		result[7] = (byte) stat;
		stat = stat >> 8;
		result[8] = (byte) len;
		len = len >> 8;
		result[9] = (byte) len;
		len = len >> 8;
		result[10] = (byte) len;
		len = len >> 8;
		result[11] = (byte) len;
		len = len >> 8;
	}

	/**
	 * 执行开启模块的操作
	 */
	private String open() {
		mod_stat_open();
		if("success".equals(receive()))
		{
			db_update_to_open();
			op.save("对模块",modName, "执行了打开的操作");//自己加的------------------
			return OPEN_SUCCESS;	
		}
		else
		{
			return OPEN_FAILED;
		}
		
	}

	/**
	 * 执行关闭模块的操作
	 */
	private String close() {
		mod_stat_close();
		if("success".equals(receive()))
		{
			db_update_to_close();
			op.save("对模块",modName, "执行了关闭的操作");//自己加的------------------
			return CLOSE_SUCCESS;
		}
		else
		{
			return CLOSE_FAILED;
		}
	}
	/**
	 * 获取本机IP和名字
	 */
	public void getip(){
		InetAddress addr;
		try {
			addr = InetAddress.getLocalHost();
			ip = addr.getHostAddress().toString();//获得本机IP
			address = addr.getHostName().toString();//获得本机名称
		} catch (UnknownHostException e) {
			e.printStackTrace();
		}
		System.out.println("ip:"+ip);
		System.out.println("addr"+address);
	}
	
	/**
	 * 发送打开模块指令
	 */
	public void mod_stat_open() {

		try {

			setMod(modId);
			setStat(1);
			len=12;
			getbyte();
			getip();
			if(Socket==null)
			{
			Socket= new DatagramSocket();
			}
			DatagramPacket dataPack = new DatagramPacket(
					result, 
					12, 
					InetAddress.getByName(ip), 
					new Integer(3000));
			try {
				Socket.send(dataPack);
			//	sendSocket.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		} catch (SocketException e) {
			e.printStackTrace();
		} catch (UnknownHostException e) {
			e.printStackTrace();
		}

	}

	/**
	 * 发送关闭模块指令
	 */
	public void mod_stat_close() {
		try {

			setMod(modId);
			setStat(0);
			len=12;
			getbyte();
			getip();
			if(Socket==null)
			{
			Socket= new DatagramSocket();
			}
			DatagramPacket dataPack = new DatagramPacket(
					result, 
					12, 
					InetAddress.getByName(ip), 
					new Integer(3000));
			try {
				Socket.send(dataPack);
				//sendSocket.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		} catch (SocketException e) {
			e.printStackTrace();
		} catch (UnknownHostException e) {
			e.printStackTrace();
		}

	}

	/**
	 * 接收底层返回信息 判断操作是否成功
	 */
	public String receive() {
		//try {
		//	DatagramSocket rec = new DatagramSocket(3000);
			//Socket= new DatagramSocket();
		
			byte b [] = new byte[8];
			DatagramPacket dprec = new DatagramPacket(b, 8);
			try {
				Socket.receive(dprec);
				strrec = new String(dprec.getData(), 0, dprec.getLength());
				Socket.close();
				
			} catch (IOException e) {
				e.printStackTrace();
			}
	//	}
		if ("success".equals(strrec)) {
			return "success";
		}
		else
		{
			return "failed";
		}
	}

	/**
	 * 将模块的数据库字段改成打开
	 */
	public void db_update_to_open() throws MessageException {

		Session session = HibernateSessionFactory.getSession();
		modStat ms = new modStat();
		ms.setId(id);
		ms.setModId(modId);
		ms.setModName(modName);
		ms.setModStat(1);
		ms.setModStat2("OPEN");
		Transaction trans = session.beginTransaction();
		// session.update(ms);
		session.merge(ms);
		trans.commit();
		HibernateSessionFactory.closeSession();
	}

	/**
	 * 将模块的数据库字段改成关闭
	 */
	public void db_update_to_close() throws MessageException {

		Session session2 = HibernateSessionFactory.getSession();
		modStat ms2 = new modStat();
		ms2.setId(id);
		ms2.setModId(modId);
		ms2.setModName(modName);
		ms2.setModStat(0);
		ms2.setModStat2("CLOSE");
		Transaction trans = session2.beginTransaction();
		// session2.update(ms2);
		session2.merge(ms2);
		trans.commit();
		HibernateSessionFactory.closeSession();
	}
}